# -*- coding: utf-8 -*-
# @Author   : niejiali
# @Time     : 2019/1/17 9:54
# @File     : decorator.py
# @Software : PyCharm
import sys
import contextlib

# AOP


class Redprint:
    def __init__(self, name):
        self.name = name
        self.verify_password_callback = None
        self.error = "xx"

    def verify_password(self, f):
        self.verify_password_callback = f
        return f

    def login(self):

        def derorator(f):
            if self.verify_password_callback:
                with self.MyException():
                    get_callback = self.verify_password_callback()
                    print(get_callback)
                    if not get_callback:
                        raise Exception("password error or not found!")

            return f
        return derorator

    @contextlib.contextmanager
    def MyException(self):
        # 上下文管理
        try:
            yield
        except Exception as e:
            print(e)
            # self.error = e
            sys.exit()


Red = Redprint('name')


@Red.verify_password
def verify_password(password_true_or_error=0):
    if not password_true_or_error:
        return False
    else:
        return True


@Red.login()
def f1():
    print("login success")


f1()

